package com.dd.web.base.entity.view;

import com.dd.web.base.entity.data.UmsMenu;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单树对象
 */
@Getter
@Setter
@ApiModel("页面菜单树对象")
public class MenuTreeNode {

    @ApiModelProperty("菜单id")
    private Long id;
    @ApiModelProperty("菜单父级id")
    private Long pId;
    @ApiModelProperty("菜单树节点显示内容")
    private String title ;
    @ApiModelProperty("是否为叶子节点")
    private boolean isLeaf;
    @ApiModelProperty("菜单树搜索所需值")
    private String value;

    public boolean getIsLeaf(){
        return this.isLeaf;
    }

    public void setIsLeaf(Boolean isLeaf){
        this.isLeaf = isLeaf;
    }

    /**
     * 将后端菜单集合转换为前台需要的菜单树集合
     * @param umsMenuList
     * @return
     */
    public static List<MenuTreeNode> transTreeNode(List<UmsMenu> umsMenuList){
        if (umsMenuList == null || (umsMenuList.size() == 0) ){
            return null;
        }
        List<MenuTreeNode> treeNodeList = new ArrayList<>();
        umsMenuList.stream().forEach(menu ->{
            MenuTreeNode mtn = new MenuTreeNode();
            mtn.setId(menu.getId());
            mtn.setPId(menu.getParentId());
            mtn.setTitle(menu.getMenuName());
            mtn.setValue(menu.getMenuName()+menu.getId());
            mtn.setIsLeaf((!menu.isHasChild()));
            treeNodeList.add(mtn);
        } );
        return treeNodeList;
    }
}
